const connection = require("../app/database");

class CommentService {
    // 创建评论
    async created(content, momentId, userId) {
        try {
            // 拼接 SQL 语句
            const statement = `INSERT INTO comment(content, moment_id, user_id) VALUES(?,?,?)`;

            // 执行 SQL 语句
            const [result] = await connection.execute(statement, [content, momentId, userId]);

            return result;
        } catch (error) {
            console.error('CommentService - created:', error);
            throw error; // 抛出异常，让调用者处理
        }
    }

    // 回复评论
    async reply(content, momentId, userId, commentId) {
        try {
            // 拼接 SQL 语句
            const statement = `INSERT INTO comment(content, moment_id, user_id, comment_id) VALUES(?,?,?,?)`;

            // 执行 SQL 语句
            const [result] = await connection.execute(statement, [content, momentId, userId, commentId]);

            return result;
        } catch (error) {
            console.error('CommentService - reply:', error);
            throw error; // 抛出异常，让调用者处理
        }
    }
}

module.exports = new CommentService();
